package com.gxuwz.medical.domain.chronicinfo;

import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.family.FamilyMember;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tools.DateUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Chronicinfo {

	/**
	 * 慢病档案编号=农合证号(14位)+建档时间（14位）
	 */
	private String id;
	/**
	 * 农合证号(家庭实体
	 */
	private FamilyMember membercode;
	/**
	 * 疾病编码（疾病实体
	 */
	private Chronicdis illcode;	
	
	/**
	 * 慢病起始时间
	 */
	private String starttime;
	/**
	 * 慢病治愈时间
	 */
	private String endtime;
	
	/**
	 * 状态:1表示正常、0表示注销
	 */
	private String status;
	/**
	 * 建档时间
	 */
	private java.util.Date creattime;
	/**
	 * 建档人
	 */
	private String creator;
	
	public Chronicinfo (){
		
	}
	
	public Chronicinfo(String id)throws Exception{
		this.id =id;
		try{
			load();
		}catch(Exception e){
			e.printStackTrace();
			throw new DbException("无法找到ID="+this.id+"对应信息");
		}
	}
	
	private void load()throws SQLException{
		Connection conn=DbUtil.getConn();
		 PreparedStatement pstmt=null;
		 ResultSet rs=null;
		try{
			
		 String sql="select * from t_chronicInfo where id=?";
		 pstmt=conn.prepareStatement(sql);
		 pstmt.setString(1, this.id);
		 rs=pstmt.executeQuery();
		 if(rs.next()){
			 	this.id = rs.getString("id");
				try {
					this.membercode = new FamilyMember(rs.getString("membercode"));
				} catch (DbException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					this.illcode = new Chronicdis(rs.getString("illcode"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.starttime = rs.getString("starttime");
				this.endtime = rs.getString("endtime");
				this.status = rs.getString("status");
				this.creattime = rs.getDate("creattime");
				this.creator = rs.getString("creator");
		 }
		}catch(SQLException e){
			throw e;
		}
		
	}
	
	/**
	 * 添加慢病证信息
	 * @throws Exception
	 */
	public void addChroInfo()throws Exception{
		Connection conn=null;
		try{
			
			Date creattime=new Date();
			String no=DateUtil.format(creattime, DateUtil.DFMT_YYYYMMDDHHMMSS);
			//慢病档案编号
			this.id=no;
			//1:获取连接实例
			conn=DbUtil.getConn();
		    //2:持久化数据
			saveToDB(conn);
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			//关闭连接
			DbUtil.close(conn);
		}
		
		
	}
	/**
	 * 持久化数据
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("insert into t_chronicInfo (id,membercode, illcode, starttime, endtime,status,creattime,creator)");
			  			   sqlBuff.append("values (?, ?, ?, ?,?, ?, ?, ?)");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.id);
			  pstmt.setString(index++, this.membercode.getMembercode());
			  pstmt.setString(index++, this.illcode.getIllcode());
			  pstmt.setString(index++, this.starttime);
			  pstmt.setString(index++, this.endtime);
			  pstmt.setString(index++, this.status);
			  java.sql.Date date=new java.sql.Date(this.creattime.getTime());  //把java.util.Date转换成java.sql.Date
			  pstmt.setDate(index++, date);
			  pstmt.setString(index++, this.creator);
			  int count =pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  e.printStackTrace();
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
}
	
	public void edit()throws SQLException{
		Connection conn =DbUtil.getConn();
		try{
			 conn.setAutoCommit(false);
			 this.updateToDB(conn);
			 conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	private void updateToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造update SQL语句字符串变量sql
			String  sql="update t_chronicInfo set illcode=?,starttime=?,endtime=? where id=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			  pstmt.setString(index++, this.illcode.getIllcode());
			  pstmt.setString(index++, this.starttime);
			  pstmt.setString(index++, this.endtime);
			  pstmt.setString(index++, this.id);
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
		}
		
	}
	
	public FamilyMember getMembercode() {
		return membercode;
	}
	public void setMembercode(FamilyMember membercode) {
		this.membercode = membercode;
	}
	public Chronicdis getIllcode() {
		return illcode;
	}
	public void setIllcode(Chronicdis illcode) {
		this.illcode = illcode;
	}
	public String getStarttime() {
		return starttime;
	}
	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
	public String getEndtime() {
		return endtime;
	}
	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public java.util.Date getCreattime() {
		return creattime;
	}
	public void setCreattime(java.util.Date creattime) {
		this.creattime = creattime;
	}
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Chronicinfo(String id, FamilyMember membercode, Chronicdis illcode,
			String starttime, String endtime, String status, Date creattime,
			String creator) {
		super();
		this.id = id;
		this.membercode = membercode;
		this.illcode = illcode;
		this.starttime = starttime;
		this.endtime = endtime;
		this.status = status;
		this.creattime = creattime;
		this.creator = creator;
	}	
	
	
}
